﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HANS_BackStage.Model.WebModel;
using HANS.Utility.Extensions;

namespace HANS_BackStageWeb.Controllers
{
    public class SharedController : Common.CommonController
    {
        // GET: Share
        [ChildActionOnly]
        public ActionResult _Menu() {
            var menuItems = AdminSession.NowMenuItems;
            var controller = Request.RequestContext.RouteData.Values["Controller"].ToString();
            var action = Request.RequestContext.RouteData.Values["Action"].ToString();
            SetMenuActionStatus(menuItems, controller, action);
            AdminSession = AdminSession;
            return PartialView(menuItems);
        }

        private bool SetMenuActionStatus(List<MenuItem> menuItems, string controller, string action)
        {

            bool hasActive = false;
            foreach (var menuItem in menuItems)
            {
                menuItem.IsActive = (menuItem.Action == action && menuItem.Controller == controller)
                                || (menuItem.Controller == controller && menuItem.Action.IsNullOrWhiteSpace());
                menuItem.IsActive |= SetMenuActionStatus(menuItem.ChildrensMenuItem, controller, action);

                hasActive |= menuItem.IsActive;
            }
            return hasActive;
        }
    }
}